<template>
  <div style='height: 100%'>
    <el-breadcrumb separator-class='el-icon-arrow-right'>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片试图区域-->
    <el-card style='height: 95%'>
      <el-row :gutter='20'>
        <el-col :span='5'>
          <el-input placeholder='请输入内容' v-model='queryInfo.query'
                    clearable @clear='getUserList'>
            <el-button slot='append' icon='el-icon-search' @click='getUserList'></el-button>
          </el-input>
        </el-col>
        <el-col :span='4'>
          <el-button type='primary' @click='dialogFormVisible = true'>
            添加用户
          </el-button>
        </el-col>
      </el-row>
      <el-dialog title="添加学生信息" :visible.sync="dialogFormVisible">
        <user-form v-on:msg="getMsg"></user-form>
      </el-dialog>

      <el-table :data='userList' border stripe height='600px'>
        <el-table-column type='index' label='编号'/>
        <el-table-column label='用户名' prop='adminName' />
        <el-table-column label='电话' prop='telephone' />
        <el-table-column label='邮箱' prop='email' />
        <el-table-column label='状态' prop='state'>
          <template slot-scope='scope'>
            <el-switch v-model='scope.row.state' :active-value=1 :inactive-value=0 @change='userStateChanged(scope.row)'></el-switch>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="queryInfo.pageSize"
        layout="sizes, prev, pager, next, jumper"
        :total="this.total"
        style='float: right'>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import UserForm from '@/components/user/userForm'
export default {
  name: 'User',
  components: { UserForm },
  data() {
    return {
      dialogFormVisible: false,
      queryInfo: {
        query: '',
        pageNum: '1',
        pageSize: '10'
      },
      userList: [],
      total: ''
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get('/api/admin', { params: this.queryInfo })
      if (res.code !== 0) {
        return this.$message.error('获取用户失败')
      }
      this.userList = res.data.records
      this.total = res.data.total
    },
    // 监听pagesize改变事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getUserList()
    },
    // 页码值改变事件
    handleCurrentChange(newNum) {
      this.queryInfo.pageNum = newNum
      this.getUserList()
    },
    async userStateChanged(userInfo) {
      const { data: res } = await this.$http.put('/api/admin/update', userInfo)
      if (res.code !== 0) {
        if (userInfo.state === 0) {
          userInfo.state = 1
        }
        if (userInfo.state === 1) {
          userInfo.state = 0
        }
        return this.$message.error('更新用户状态失败')
      }
      this.$message.success('更新用户状态成功')
    },
    getMsg: function(val) {
      this.dialogFormVisible = false
      this.getUserList()
    }
  }
}
</script>

<style scoped>

</style>
